#include "user.h"
#include "ui_user.h"

User::User(User_t user, QWidget *parent) :
    QWidget(parent),user(user),
    ui(new Ui::User)
{
    welog_debug() << "user";
    ui->setupUi(this);
    setFixedSize(800, 480);

    this->setAttribute(Qt::WA_StyledBackground, true);
    this->setObjectName("widget");
    this->setStyleSheet("#widget{background-image: url(:/res/beijing.jpg);}");

    ui->name_label->setText(QString("欢迎您，%1用户").arg(user.name));
    ui->money_label->setText("您目前的积分还有：" + QString::number(user.money));
#if 0
    ui->listWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    ui->listWidget->setVerticalScrollMode(QListWidget::ScrollPerPixel);
    ui->listWidget->setFrameShape(QListWidget::NoFrame);
    ui->listWidget->setResizeMode(QListWidget::Adjust);
    ui->listWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    ui->listWidget->setFocusPolicy(Qt::NoFocus);
    ui->listWidget->setViewMode(QListWidget::ListMode);
    ui->listWidget->setGridSize(QSize(590, 80));
    ui->listWidget->setFlow(QListWidget::TopToBottom);
    QScroller::grabGesture(ui->listWidget, QScroller::TouchGesture);
    connect(ui->listWidget, &QListWidget::itemClicked, this, &User::itemClicked);

    QList<Book_t> list = Sqlite::Get()->Select_book();
    foreach(Book_t book, list)
    {
        listWidgetAddItem(book.name, book.sell, book.num);
    }
#endif
}

User::~User()
{
    delete ui;
}

void User::listWidgetAddItem(QString book, int sell, int num)
{
#if 0
    QListWidgetItem *item = new QListWidgetItem(ui->listWidget);
    item->setData(NameRole, book);
    item->setData(SellRole, sell);
    item->setData(NumRole, num);
    item->setSizeHint(QSize(590, 80));
    QSize size = item->sizeHint();

    QWidget *widget = new QWidget();
    widget->setFixedSize(size);

    QLabel *icon_label = new QLabel(book, widget);
    icon_label->setFixedSize(300, 80);
    icon_label->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
    icon_label->setStyleSheet("color:#000;font-size:30px;");
    icon_label->move(0,0);

    QPushButton *btn = new QPushButton("借出", widget);
    btn->setFixedSize(90, 80);
    btn->setStyleSheet("QPushButton{color:#fff;font-size:30px;}");
    btn->setProperty("name", book);
    btn->setProperty("num", num);
    btn->setProperty("sell", sell);
    connect(btn, &QPushButton::clicked, this, &User::sendBtn_click);
    btn->move(300, 0);

    QPushButton *msg = new QPushButton("详细信息", widget);
    msg->setFixedSize(170, 80);
    msg->setStyleSheet("QPushButton{color:#fff;font-size:30px;}");
    msg->setObjectName("msgBtn");
    msg->setProperty("name", book);
    msg->setProperty("num", num);
    msg->setProperty("sell", sell);
    connect(msg, &QPushButton::clicked, this, &User::msgBtn_click);
    msg->move(400, 0);


    ui->listWidget->addItem(item);
    ui->listWidget->setItemWidget(item, widget);
#endif
}

void User::closeEvent(QCloseEvent *)
{
    emit sigClose();
}


void User::itemClicked(QListWidgetItem *)
{
    qDebug() << "itemClicked";
}

void User::on_pushButton_clicked()
{
    this->close();

}

void User::sendBtn_click()
{

}

void User::msgBtn_click()
{

}

void User::on_pushButton_2_clicked()
{
    Lend *l = new Lend(user, 0);
    connect(l, &Lend::deleteMoney, this, [&](int num)
    {
        user.money = num;
        ui->money_label->setText("您目前的积分还有：" + QString::number(num));
    });
    l->show();
}

void User::on_pushButton_3_clicked()
{
    Lend *l = new Lend(user, 1);
    connect(l, &Lend::sigMoney, this, [&](int num)
    {
        user.money = num;
        ui->money_label->setText("您目前的积分还有：" + QString::number(num));
    });
    l->show();
}
